public class MyLinkedList implements IMyLinkedList {
    static class ListNode {
        public int val;
        public ListNode prev;
        public ListNode next;
        public ListNode (int val){
            this.val = val;
        }
    }

    public ListNode head;
    public ListNode last;

    @Override
    public void addFirst(int data) {
        ListNode newNode = new ListNode(data);
        if(head == null){
            head = last = newNode;
        }else{
            newNode.next = head;
            head.prev = newNode;
            head = newNode;
        }
    }

    @Override
    public void addLast(int data) {
        ListNode newNode = new ListNode(data);
        if(head == null){
            head = last = newNode;
        }else{
            newNode.prev = last;
            last.next = newNode;
            last = newNode;
        }

    }

    @Override
    public void addIndex(int index, int data) {
        //判断index的合法性
        try{
            checkIndex(index);
        }catch(IndexNotLegalException e){
            e.printStackTrace();
        }
        if(index == 0){
            addFirst(data);
            return;
        }
        if(index == size()){
            addLast(data);
            return;
        }
        ListNode newNode = new ListNode(data);
        ListNode cur = head;
        while(index != 0){
            cur = cur.next;
            index--;
        }
        newNode.next = cur;
        newNode.prev = cur.prev;
        cur.prev.next = newNode;
        cur.prev = newNode;
    }
    private void checkIndex(int index){
        if(index<0 || index>size()){
            throw new IndexNotLegalException("Index不合法...");
        }
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    @Override
    public void remove(int key) {
        ListNode cur = head;
        if(head.val == key && head == last){
            head = last = null;
            return;
        }
        //如果删除的在head节点
        if(head.val == key){
            head = head.next;
            head.prev = null;
            return;
        }
        while(cur != null){
            if(cur.val == key){
                cur.prev.next = cur.next;
                if(cur.next == null){
                    last = cur.prev;
                    break;
                }
                cur.next.prev = cur.prev;
            }
            cur = cur.next;
        }
    }

    @Override
    public void removeAllKey(int key) {
        ListNode cur = head;
        if(head.val == key && head == last){
            head = last = null;
            return;
        }
        //如果删除的在head节点
        if(head.val == key){
            head = head.next;
            head.prev = null;
        }
        while(cur != null){
            if(cur.val == key){
                cur.prev.next = cur.next;
                if(cur.next == null){
                    last = cur.prev;
                    break;
                }
                cur.next.prev = cur.prev;
            }
            cur = cur.next;
        }
    }

    @Override
    public int size() {
        int count = 0;
        ListNode cur = head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    @Override
    public void display() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    @Override
    public void clear() {
        ListNode cur = head;
        while(cur != null){
            ListNode curN = cur.next;
            cur.prev = null;
            cur.next = null;
            cur = curN;
        }
    }
}
